import { GetUserInfo } from '@/api/user';
import Taro from '@tarojs/taro';
import { useRequest } from 'taro-hooks';

// 用来限制用户的行为
export default function useUserExamine() {
  const { data: user } = useRequest(GetUserInfo, {
    cacheKey: 'get:user:info',
  });

  function isUserExamine() {
    // !审核失败
    if (user?.status === 2) {
      Taro.redirectTo({ url: `/pages/info/fail` });
      return Promise.reject(false);
    }

    // ?审核中
    if (user?.status === 0) {
      Taro.redirectTo({
        url: user.name ? `/pages/info/success` : `/pages/info/index`,
      });
      return Promise.reject(false);
    }

    //未登录
    if (!user || !user?.id) {
      Taro.redirectTo({ url: `/pages/login/role` });
      return Promise.reject(false);
    }

    // *被禁用
    if (user?.status === 3) {
      Taro.showModal({
        title: '系统提示',
        content: `该账号已被禁用,请联系平台客服`,
        showCancel: false,
      });
      return Promise.reject(false);
    }

    return Promise.resolve(true);
  }

  return {
    isUserExamine,
  };
}
